package com.cloud.utils;

import cn.afterturn.easypoi.entity.ImageEntity;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import java.io.File;

/**
 * 设置统计图表工具
 *
 * @author yzj
 */
public class ChartUtils {

    private static FreeChartUtils freeChartUtils = new FreeChartUtils();

    /**
     * 设置图片插入
     *
     * @param height 图片高
     * @param width  图片宽
     * @param url    图片地址
     */
    public static ImageEntity setImage(int height, int width, String url) {
        ImageEntity image = new ImageEntity();
        image.setHeight(height);
        image.setWidth(width);
        image.setUrl(url);
        image.setType(ImageEntity.URL);
        return image;
    }

    /**
     * 设置饼图插入
     *
     * @param height  图表高
     * @param width   图表宽
     * @param title   图表标题
     * @param dataSet 设置参数/调用addValue()
     */
    public static ImageEntity setPieChart(int height, int width, String title, DefaultPieDataset dataSet) {
        ImageEntity image = new ImageEntity();
        try {
            File file = File.createTempFile("chart_", ".jpg");
            freeChartUtils.createPieChart(dataSet, file, title);
            image.setHeight(height);
            image.setWidth(width);
            image.setUrl(file.getAbsolutePath());
            image.setType(ImageEntity.URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 设置环形图插入
     *
     * @param height  图表高
     * @param width   图表宽
     * @param title   图表标题
     * @param dataSet 设置参数/调用addValue()
     */
    public static ImageEntity setRingChart(int height, int width, String title, DefaultPieDataset dataSet) {
        ImageEntity image = new ImageEntity();
        try {
            File file = File.createTempFile("chart_", ".jpg");
            freeChartUtils.createRingChart(dataSet, file, title);
            image.setHeight(height);
            image.setWidth(width);
            image.setUrl(file.getAbsolutePath());
            image.setType(ImageEntity.URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 设置3D饼图插入
     *
     * @param height  图表高
     * @param width   图表宽
     * @param title   图表标题
     * @param dataSet 设置参数/调用addValue()
     */
    public static ImageEntity set3DPieChart(int height, int width, String title, DefaultPieDataset dataSet) {
        ImageEntity image = new ImageEntity();
        try {
            File file = File.createTempFile("chart_", ".jpg");
            freeChartUtils.create3DPieChart(dataSet, file, title);
            image.setHeight(height);
            image.setWidth(width);
            image.setUrl(file.getAbsolutePath());
            image.setType(ImageEntity.URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }


    /**
     * 设置条形图插入
     *
     * @param height  图表高
     * @param width   图表宽
     * @param title   图表标题
     * @param xAxis   X轴标签
     * @param yAxis   Y轴标签
     * @param dataSet 设置参数/调用addValue()
     * @return
     */
    public static ImageEntity setBarChart(int height, int width, String title,
                                          String xAxis, String yAxis, DefaultCategoryDataset dataSet) {
        ImageEntity image = new ImageEntity();
        try {
            File file = File.createTempFile("chart_", ".jpg");
            freeChartUtils.createBarChart(title, xAxis, yAxis, file, dataSet, PlotOrientation.HORIZONTAL);
            image.setHeight(height);
            image.setWidth(width);
            image.setUrl(file.getAbsolutePath());
            image.setType(ImageEntity.URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 设置堆积条形图插入
     *
     * @param height  图表高
     * @param width   图表宽
     * @param title   图表标题
     * @param xAxis   X轴标签
     * @param yAxis   Y轴标签
     * @param dataSet 设置参数/调用addValue()
     * @return
     */
    public static ImageEntity setHeapBarChart(int height, int width, String title,
                                              String xAxis, String yAxis, DefaultCategoryDataset dataSet) {
        ImageEntity image = new ImageEntity();
        try {
            File file = File.createTempFile("chart_", ".jpg");
            freeChartUtils.createHeapBarChart(title, xAxis, yAxis, file, dataSet, PlotOrientation.HORIZONTAL);
            image.setHeight(height);
            image.setWidth(width);
            image.setUrl(file.getAbsolutePath());
            image.setType(ImageEntity.URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

    /**
     * 设置折线图插入
     *
     * @param height  图表高
     * @param width   图表宽
     * @param title   图表标题
     * @param xAxis   X轴标签
     * @param yAxis   Y轴标签
     * @param dataSet 设置参数/调用addValue()
     * @return
     */
    public static ImageEntity setLineChart(int height, int width, String title,
                                           String xAxis, String yAxis, DefaultCategoryDataset dataSet) {
        ImageEntity image = new ImageEntity();
        try {
            File file = File.createTempFile("chart_", ".jpg");
            freeChartUtils.createLineChart(title, xAxis, yAxis, file, dataSet, PlotOrientation.HORIZONTAL);
            image.setHeight(height);
            image.setWidth(width);
            image.setUrl(file.getAbsolutePath());
            image.setType(ImageEntity.URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return image;
    }

}
